//
//  Index_LiveViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/1.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "Index_LiveViewController.h"
#import "SGPagingView.h"
#import "SGPageContentCollectionView.h"
#import "Index_Live_ListViewController.h"
#import "Host_Detail_Ablum_ViewController.h"
#import "LiveCatogryViewController.h"
#import "AreaViewController.h"
#import "RechargeViewController.h"

@interface Index_LiveViewController ()<SGPageTitleViewDelegate, SGPageContentCollectionViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@property (weak, nonatomic) IBOutlet UILabel *nick;
@property (weak, nonatomic) IBOutlet UIImageView *icon;

- (IBAction)selectCity:(UIButton *)sender;
@property (nonatomic, strong) SGPageContentCollectionView *pageContentCollectionView;
@property(nonatomic,strong)NSMutableArray *cat_Arr;
@property(nonatomic,strong)MBProgressHUD *HUD;
@end

@implementation Index_LiveViewController
-(NSMutableArray *)cat_Arr{
    if(!_cat_Arr){
        _cat_Arr = [NSMutableArray array];
    }
    return _cat_Arr;
}
-(void)viewWillAppear:(BOOL)animated{
    if([XTool GetDefaultInfo:USER_INFO]){
        NSDictionary *userDic = [XTool GetDefaultInfo:USER_INFO];
        self.level.text = [NSString stringWithFormat:@"%@",userDic[@"group_name"]];
        NSURL *url = [NSURL URLWithString:userDic[@"avatar"]];
        [self.icon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Bitmap.png"]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if([XTool GetDefaultInfo:LOCATION]){
        [self.city_Btn setTitle:[XTool GetDefaultInfo:LOCATION] forState:UIControlStateNormal];
    }
    [self loadCat];
    // Do any additional setup after loading the view from its nib.
}
-(void)hideKb{
    [self.view endEditing:1];
}
#pragma mark 获取直播分类
-(void)loadCat{
   self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
//    [self.view addSubview:_HUD];
//    _HUD.label.text = @"数据获取中";
//    //细节文字
//    _HUD.detailsLabel.text = @"请耐心等待";
//    //显示对话框
//  [_HUD showAnimated:YES];
    NSDictionary *param = @{@"ac":@"member",@"op":@"member",@"ctrl":@"live",@"ta":@"category"};
    
    [self weqPostHandle:param success:^(id response) {
        [self.HUD removeFromSuperview];
        if(response){
            [self.cat_Arr addObjectsFromArray:response[@"data"]];
            NSLog(@"首页分类数据===%@",response);
            [self setupPageView];
        }
    } failure:^(NSError *error) {
         [self.HUD removeFromSuperview];
    } hasProgress:1];
}
//键盘搜索键事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:1];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"search" object:textField.text];
    return YES;
}
//筛选
- (IBAction)filter:(UIButton *)sender {
    LiveCatogryViewController *vc = [[LiveCatogryViewController alloc]init];
    vc.filter=^(NSMutableDictionary *dic){
      
        [[NSNotificationCenter defaultCenter]postNotificationName:@"filter" object:dic];
    };
    [self pushSecondVC:vc];
}
- (void)setupPageView {
    self.search_Tf.delegate = self;
    self.icon.layer.cornerRadius = 15;
    self.icon.layer.masksToBounds = 1;
    self.search_View.layer.cornerRadius = 19;
    self.search_View.layer.masksToBounds = 1;
    CGFloat statusHeight = CGRectGetHeight([UIApplication sharedApplication].statusBarFrame);
    CGFloat pageTitleViewY = 0;
    NSMutableArray *titless = [NSMutableArray array];
    for(int i =0;i<self.cat_Arr.count;i++){
        [ titless addObject:self.cat_Arr[i][@"title"]];
    }
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.titleColor = [UIColor lightGrayColor];
    // configure.titleFont = [UIFont systemFontOfSize:20];
    configure.titleSelectedColor = [UIColor blackColor];
    configure.indicatorColor = BUTTON_COLOR;
    //    configure.indicatorColor = [UIColor blackColor];
    //  configure.indicatorAdditionalWidth = 80; // 说明：指示器额外增加的宽度，不设置，指示器宽度为标题文字宽度；若设置无限大，则指示器宽度为按钮宽度
    configure.titleGradientEffect = YES;
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 80, screen_width, 44) delegate:self titleNames:titless configure:configure];
    [self.view addSubview:_pageTitleView];
    NSMutableArray *vcArr = [NSMutableArray array];
    for(int i=0;i<titless.count;i++){
        Index_Live_ListViewController *vc = [[Index_Live_ListViewController alloc]init];
        vc.cat_id = [NSString stringWithFormat:@"%@",self.cat_Arr[i][@"id"]];
        [vcArr addObject:vc];
    }

    NSArray *childArr = [NSArray arrayWithArray:vcArr];
    /// pageContentCollectionView
    CGFloat ContentCollectionViewHeight = screen_height - 187;//内嵌的子控制器的视图高度
    self.pageContentCollectionView = [[SGPageContentCollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_pageTitleView.frame), screen_width, ContentCollectionViewHeight) parentVC:self childVCs:childArr];
    _pageContentCollectionView.delegatePageContentCollectionView = self;
    [self.view addSubview:_pageContentCollectionView];
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    [self.view endEditing:1];
    [self.pageContentCollectionView setPageContentCollectionViewCurrentIndex:selectedIndex];
}

- (void)pageContentCollectionView:(SGPageContentCollectionView *)pageContentCollectionView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
     [self.view endEditing:1];
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (IBAction)selectCity:(UIButton *)sender {
    AreaViewController *vc = [[AreaViewController alloc]init];
    vc.city=^(NSDictionary *dic){
        [self.city_Btn setTitle:dic[@"name"] forState:UIControlStateNormal];
        //后续根据区域选择直播
          [[NSNotificationCenter defaultCenter]postNotificationName:@"citys" object:dic];
    };
    [self pushSecondVC:vc];
}
- (IBAction)recharge:(UIButton *)sender {
    RechargeViewController *vc = [[RechargeViewController alloc]init];
    [self pushSecondVC:vc];
}
@end
